package com.hluck.calculatertest.ui.viewmodels

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import java.util.Random


class MyViewModel(
    private val application: Application,
    private val savedStateHandle: SavedStateHandle
) : AndroidViewModel(application) {

    private lateinit var config: SharedPreferences

    companion object {
        const val KEY_HIGH_SCORE = "key_high_score"
        const val KEY_LEFT_NUMBER = "key_left_number"
        const val KEY_RIGHT_NUMBER = "key_right_number"
        const val KEY_OPERATOR = "key_operator"
        const val KEY_ANSWER = "key_answer"
        const val SAVE_SP_NAME = "config"
        const val KEY_CURRENT_SCORE = "key_current_score"
    }
    var isWin: Boolean = false

    init {
        if (!savedStateHandle.contains(KEY_HIGH_SCORE)) {
            config = application.getSharedPreferences(SAVE_SP_NAME, Context.MODE_PRIVATE)
            savedStateHandle[KEY_HIGH_SCORE] = config.getInt(KEY_HIGH_SCORE, 0)
            savedStateHandle[KEY_LEFT_NUMBER] = 0
            savedStateHandle[KEY_RIGHT_NUMBER] = 0
            savedStateHandle[KEY_OPERATOR] = "+"
            savedStateHandle[KEY_ANSWER] = 0
            savedStateHandle[KEY_CURRENT_SCORE] = 0
        }
    }

    fun leftNumber(): MutableLiveData<Int> {
        return savedStateHandle.getLiveData(KEY_LEFT_NUMBER)
    }

    fun rightNumber(): MutableLiveData<Int> {
        return savedStateHandle.getLiveData(KEY_RIGHT_NUMBER)
    }

    fun operator(): MutableLiveData<String> {
        return savedStateHandle.getLiveData(KEY_OPERATOR)
    }

    fun highScore(): MutableLiveData<Int> {
        return savedStateHandle.getLiveData(KEY_HIGH_SCORE)
    }

    fun currentScore(): MutableLiveData<Int> {
        return savedStateHandle.getLiveData(KEY_CURRENT_SCORE)
    }

    fun answer(): MutableLiveData<Int> {
        return savedStateHandle.getLiveData(KEY_ANSWER)
    }

    fun generator() {
        val level = 20
        val random = Random()
        var x = random.nextInt(level) + 1
        var y = random.nextInt(level) + 1

        if (x % 2 == 0) {
            operator().value = "+"
            if (x > y) {
                answer().value = x
                leftNumber().value = y
                rightNumber().value = x - y
            } else {
                answer().value = y
                leftNumber().value = x
                rightNumber().value = y - x
            }
        } else {
            operator().value = "-"
            if (x > y) {
                answer().value = x - y
                leftNumber().value = x
                rightNumber().value = y
            } else {
                answer().value = y - x
                leftNumber().value = y
                rightNumber().value = x
            }
        }
    }

    fun saveHighScore() {
        config.edit {
            putInt(KEY_HIGH_SCORE, highScore().value ?: 0)
        }
    }

    fun answerCorrect(){
        currentScore().value = currentScore().value?.plus(1)
        if (currentScore().value!! > highScore().value!!) {
            highScore().value = currentScore().value
            isWin = true
        }
        generator()
    }
}